package com.Utility;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.os.AsyncTask;

import com.events.WebServiceCommunicatorListener;

public class WebServiceCommunicator extends AsyncTask<Void, Integer, String> {

	public static final int TIME_OUT = 15000;
	public static final int SOCKET_TIME_OUT = 30000;

	private WebServiceCommunicatorListener listener;
	private String data;
	private WebServiceFlag flag;
	private Context cxt;
	private String result;

	public WebServiceCommunicator(Context cxt) {
		this.cxt = cxt;
	}

	public void fetch(WebServiceFlag flag, String data) {
		this.data = data;
		this.flag = flag;
		this.execute();
	}

	@Override
	protected void onPreExecute() {
		// Log.i("AAAAAA", "pre");
		if (listener != null) {
			listener.onConnectionOpen(flag);
		}
	}

	public void setWebServiceCommuncatorListener(
			WebServiceCommunicatorListener listener) {
		this.listener = listener;
	}

	@Override
	protected String doInBackground(Void... params) {
		// Log.i("AAAAAA", "0");
		HttpParams httpParameters = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParameters, TIME_OUT);
		HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIME_OUT);
		HttpClient client = new DefaultHttpClient(httpParameters);
		HttpPost method = new HttpPost(Configs.API_BASE_URL);
		try {
			Header[] headers = getHeader(flag);
			method.setHeaders(headers);
			method.setEntity(new ByteArrayEntity(data.getBytes()));
			HttpResponse response = client.execute(method);
			// result= response.getEntity().getContent();
			result = EntityUtils.toString(response.getEntity(), "UTF-8");
			return "OK";
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return "Unknown Protocol Used.";
		} catch (IOException e) {
			e.printStackTrace();
			return "No connection to the server.";
		} catch (Exception e) {
			e.printStackTrace();
			return "Unknown exception.";
		}

	}

	@Override
	protected void onPostExecute(String check) {
		if ("OK".equals(check) && result != null) {
			if (listener != null)
				listener.onConnectionDone(flag, result);
		} else if (listener != null) {
			listener.onConnectionError(flag,
					"Cannot connect to server. Please check your connection.");
		}
	}

	public static Header[] getHeader(WebServiceFlag fl) {
		if (fl == WebServiceFlag.GET_CONTACT) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetContacts\"") };
		} else if (fl == WebServiceFlag.GET_CONTACT_INFOR) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetContactInfo\"") };
		} else if (fl == WebServiceFlag.GET_GROUP) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetGroups\"") };
		} else if (fl == WebServiceFlag.GET_GROUP_NAME) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetGroupName\"") };
		} else if (fl == WebServiceFlag.DELETE_CONTACT) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/DeleteContactsFromList\"") };
		} else if (fl == WebServiceFlag.ADD_GROUP) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/AddGroupName\"") };
		} else if (fl == WebServiceFlag.ADD_CONTACT_EXTEND) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/AddContact_Ext\"") };
		} else if (fl == WebServiceFlag.IMPORT_GMAIL) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/ImportGMailContacts\"") };
		} else if (fl == WebServiceFlag.GET_SECURE) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetSentSecurePackagesReport\"") };
		} else if (fl == WebServiceFlag.GET_COUNTRY) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetCountrySMSCodes\"") };
		} else if (fl == WebServiceFlag.EDIT_GROUP_NAME) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/UpdateGroupName\"") };
		} else if (fl == WebServiceFlag.ADD_GROUP_MEMBER) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/AddGroupMember\"") };
		} else if (fl == WebServiceFlag.DELETE_GROUP) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/DeleteGroupsFromList\"") };
		} else if (fl == WebServiceFlag.DELETE_GROUP_MEMBER) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/DeleteGroupMembers\"") };
		} else if (fl == WebServiceFlag.GET_GROUP_MEMBER) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetGroupMembers\"") };
		} else if (fl == WebServiceFlag.SEND_ADVISORY) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/SendAdvisory\"") };
		} else if (fl == WebServiceFlag.GET_WEB_INTERFACE_VAULT_URL) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetWebInterfaceVaultURL\"") };
		} else if (fl == WebServiceFlag.GET_VAULT_CATALOG) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetVaultCatalog\"") };
		} else if (fl == WebServiceFlag.GET_VAULT_STATISTIC) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetVaultStatistics\"") };
		} else if (fl == WebServiceFlag.DELETE_VAULT_FILE) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/DeleteFilesFromVault\"") };
		} else if (fl == WebServiceFlag.ADD_CONTACT) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/AddContact\"") };
		}else if (fl == WebServiceFlag.SEND_MAIL) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/SendEPackage_EXT\"") };
		} else if (fl == WebServiceFlag.GET_OUTBOX) {
			return new Header[] {
					new BasicHeader("Content-Type", "text/xml; charset=utf-8"),
					new BasicHeader("SOAPAction",
							"\"https://www.privacydatasolutions.net/GetUserOutbox\"") };
		} else
			return null;
	}

	public enum WebServiceFlag {
		GET_OUTBOX, GET_CONTACT, GET_CONTACT_INFOR, GET_GROUP, GET_GROUP_NAME, DELETE_CONTACT,ADD_CONTACT,  ADD_CONTACT_EXTEND, ADD_GROUP, DELETE_GROUP, EDIT_GROUP_NAME, IMPORT_GMAIL, GET_SECURE, GET_COUNTRY, ADD_GROUP_MEMBER, DELETE_GROUP_MEMBER, GET_GROUP_MEMBER, SEND_ADVISORY, GET_WEB_INTERFACE_VAULT_URL, GET_VAULT_CATALOG, GET_VAULT_STATISTIC, DELETE_VAULT_FILE, SEND_MAIL
	}
}
